<template>
  <div class="voice-message">
    <div class="voice-content" @click="handlePlayVoice">
      <i class="el-icon-microphone" :class="microphoneIconClass"></i>
      <span class="voice-duration">{{ getVoiceDuration() }}</span>
      <i
        v-if="isPlaying"
        class="el-icon-loading el-icon--right"
        style="animation: rotate 1.5s linear infinite"
      ></i>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VoiceMessage',
  props: {
    message: {
      type: Object,
      required: true
    },
    playingVoiceId: {
      type: [String, Number],
      default: null
    }
  },
  computed: {
    isPlaying() {
      return this.playingVoiceId === this.message.id;
    },
    microphoneIconClass() {
      return this.message.isMyselfMsg ? 'microphone-icon-green' : 'microphone-icon-blue';
    }
  },
  methods: {
    // 获取语音时长
    getVoiceDuration() {
      return this.message.voiceDuration || "00:00";
    },
    
    // 播放语音
    handlePlayVoice() {
      this.$emit('play-voice', this.message.voiceUrl, this.message.id);
    }
  }
}
</script>

<style scoped>
.voice-message {
  display: inline-block;
}

.voice-content {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: #f0f0f0;
  border-radius: 8px;
  cursor: pointer;
  min-width: 80px;
  max-width: 200px;
}

.voice-content:hover {
  background: #e0e0e0;
}

.microphone-icon-green {
  color: #67c23a;
  margin-right: 8px;
}

.microphone-icon-blue {
  color: #409eff;
  margin-right: 8px;
}

.voice-duration {
  font-size: 14px;
  color: #333;
  flex: 1;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>